package com.nchuoj.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nchuoj.answer.mapper.AnswerQuestionMapper;
import com.nchuoj.answer.pojo.AnswerQuestion;
import com.nchuoj.answer.service.AnswerQuestionService;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author lenovo
 * @description 针对表【answer_question】的数据库操作Service实现
 * @createDate 2022-12-24 18:22:14
 */
@Service
public class AnswerQuestionServiceImpl extends ServiceImpl<AnswerQuestionMapper, AnswerQuestion>
        implements AnswerQuestionService {

    /***
     * 创建一条答题记录
     * @param userId 用户id
     * @param questionId 题目id
     * @param questionSetId 题目集id
     * @param code 代码
     * @param sum 总分
     * @return 创建的对象
     */
    @Override
    public AnswerQuestion record(Integer userId, Integer questionId, Integer questionSetId, String code, Integer sum) {
        AnswerQuestion aq = new AnswerQuestion();
        aq.setUserId(userId);
        aq.setQuestionId(questionId);
        aq.setQuestionSetId(questionSetId);
        aq.setCode(code);
        aq.setScore(sum);
        this.saveOrUpdate(aq);
        LambdaQueryWrapper<AnswerQuestion> wrapper = new LambdaQueryWrapper<>();
        return Optional.ofNullable(this.getOne(wrapper)).orElse(new AnswerQuestion());
    }
}




